<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 卡片头部 -->
            <div slot="header">
                <el-row :gutter="20">
                    <el-col :span="7">
                        <el-input placeholder="请输入商品名称" clearable v-model="queryInfo.query" @clear="getGoodsList">
                            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"
                                @keyup.enter="keyDown(e)"></el-button>
                        </el-input>
                    </el-col>
                    <el-col :span="6">
                        <el-button type="primary" @click="addGoods">添加商品</el-button>
                    </el-col>
                </el-row>
            </div>

            <!-- 卡片主体 -->
            <div>
                <!-- 表格区域 -->
                <el-table :data="goodsList" border stripe>
                    <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
                    <el-table-column prop="goods_name" label="商品名称" width="690"></el-table-column>
                    <el-table-column prop="goods_price" label="商品价格（元）" align="center"></el-table-column>
                    <el-table-column prop="goods_weight" label="商品重量" align="center"></el-table-column>
                    <el-table-column prop="add_time" label="创建时间" width="180" align="center">
                        <template slot-scope="scope">
                            {{scope.row.add_time | dateFormat}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="150">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="editGoods(scope.row.goods_id)">
                                编辑
                            </el-button>
                            <el-button size="mini" type="danger" @click="deleteGoods(scope.row.goods_id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页 -->
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="goodsTotal" background>
                </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            // 商品列表
            goodsList: [],
            // 获取分类列表数据所需参数
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            // 商品列表数据总条数
            goodsTotal: 0,
        }
    },
    created() {
        // 获取商品列表
        this.getGoodsList();
    },
    methods: {
        // 获取商品列表
        async getGoodsList() {
            const { data: res } = await this.$http.get('goods', { params: this.queryInfo });
            if (res.meta.status !== 200) return this.$message.error('商品列表获取失败');
            this.goodsList = res.data.goods;
            this.goodsTotal = res.data.total;
        },
        // 处理每页显示条数的变化
        handleSizeChange(val) {
            this.queryInfo.pagesize = val;
            this.getGoodsList();
        },
        // 处理当前页码的变化
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val;
            this.getGoodsList();
        },
        // 回车键搜索
        keyDown(e) {
            //如果是回车则执行搜索方法
            if (e.keyCode == 13) {
                this.getGoodsList();
            }
        },
        // 删除商品
        deleteGoods(goods_id) {
            this.$confirm(`此操作将删除该商品, 是否继续?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const { data: res } = await this.$http.delete('goods/' + goods_id);
                if (res.meta.status !== 200) return this.$message.error('删除商品失败');
                this.$message({
                    type: 'success',
                    message: '删除商品成功!'
                });
                // 重新获取商品列表
                this.getGoodsList();
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 添加商品
        addGoods() {
            this.$router.push('/goods/add');
        },
        // 编辑商品
        editGoods(goods_id) {
            this.$router.push({ path: '/goods/edit', query: { goods_id: goods_id } });
        }
    },
    // 回车键搜索 绑定监听事件
    mounted() {
        window.addEventListener('keydown', this.keyDown);
    },
    // 回车键搜索 摧毁事件
    destroyed() {
        window.removeEventListener('keydown', this.keyDown, false);
    }
}
</script>

<style lang="less" scoped>

</style>
